package com.idcheck.rechcustoms;
import android.os.Build;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.LinearLayoutCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import okhttp3.RequestBody;
import utils.PrefUtils;


/**
 * author: sunshine
 * created on: 2021/2/20 16:58
 * description:
 */
public class AppointmentListActivity extends AppCompatActivity {
    TextView tvNoData;
    LinearLayout tv_back;
    XRecyclerView rvData;
    private List<OrderModel> mList = new ArrayList<>();
    private OrderListAdapter mAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_appointment_list);

        if (getSupportActionBar() != null) {

            getSupportActionBar().hide();

        }
        hideBottomUIMenu();

        tvNoData = (TextView)findViewById(R.id.tv_no_data);
        rvData = (XRecyclerView) findViewById(R.id.rv_data);
        tv_back = (LinearLayout) findViewById(R.id.ll_back);
        tv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Message message = new Message();
                message.what=2200;
                EventBus.getDefault().post(message);
                finish();
            }
        });
        //WaitDialog.show(KeyListActivity.this, "网络请求中，请稍后...");

        //eventbus
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }

        getData();
    }

    protected void hideBottomUIMenu() {
        //隐藏虚拟按键，并且全屏
        if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) { // lower api
            View v = this.getWindow().getDecorView();
            v.setSystemUiVisibility(View.GONE);
        } else if (Build.VERSION.SDK_INT >= 19) {

            Window _window = getWindow();
            WindowManager.LayoutParams params = _window.getAttributes();
            params.systemUiVisibility = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION|View.SYSTEM_UI_FLAG_IMMERSIVE;
            _window.setAttributes(params);
        }
    }

    private void getData() {
        new FetchOrderListTask(PrefUtils.getString(AppointmentListActivity.this, "adminid", "0")).execute();
    }

    @Subscribe(threadMode = ThreadMode.MAIN,priority = 100,sticky = false)     //在UI线程执行，100  为优先级
    public void onEvent(Message message) {
        switch (message.what) {
            case 1800:
                EventMessageForOrderList(message.obj);
                break;
            case 1900:
                EventMessageForYuYueOpen(message.obj);
                break;
            default:
                //EventMessageForError(message.what);
                break;

        }
    }

    private void EventMessageForYuYueOpen(Object obj) {
        try{
            JSONObject result = new JSONObject(obj.toString());
            Toast.makeText(AppointmentListActivity.this, result.getString("msg"), Toast.LENGTH_SHORT).show();
        }catch(Exception e){
        }
    }

    private void EventMessageForOrderList(Object obj) {
        mList.clear();
        //mList = obj.toString().getJSONObject("data");
        try{
            JSONObject result = new JSONObject(obj.toString());
            if(result.getString("code").equals("100")){
                JSONArray orders = result.getJSONArray("data");
                for(int i=0; i<orders.length(); i++){
                    JSONObject jsonObject1 = orders.getJSONObject(i);
                    OrderModel addressModule = new OrderModel();
                    addressModule.setAdd_at(jsonObject1.get("add_at").toString());
                    addressModule.setId(jsonObject1.get("id").toString());
                    addressModule.setCompany(jsonObject1.get("company").toString());
                    addressModule.setGridnum(jsonObject1.get("gridnum").toString());
                    addressModule.setNum(jsonObject1.get("num").toString());
                    mList.add(addressModule);
                }

                LinearLayoutManager manager = new LinearLayoutManager(AppointmentListActivity.this);
                rvData.setLayoutManager(manager);
                mAdapter = new OrderListAdapter(AppointmentListActivity.this, mList);
                rvData.setAdapter(mAdapter);
                rvData.setPullRefreshEnabled(false);
                rvData.setLoadingMoreEnabled(false);
                mAdapter.setViewClickListener(new OrderListAdapter.ViewClickListener() {
                    @Override
                    public void borrowKey(int pos) {
                        new FetchYuyueOpenGridTask(mList.get(pos).getId(), PrefUtils.getString(AppointmentListActivity.this, "adminid", "0")).execute();

                    }
                });

                if (mList.size() == 0) {
                    tvNoData.setVisibility(View.VISIBLE);
                }
            } else {
                Toast.makeText(AppointmentListActivity.this, result.getString("msg"), Toast.LENGTH_SHORT).show();
            }


        }catch(Exception e){
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
